<?php

use Fusonic\SpreadsheetExport\Spreadsheet;
use Fusonic\SpreadsheetExport\ColumnTypes\CurrencyColumn;
use Fusonic\SpreadsheetExport\ColumnTypes\DateColumn;
use Fusonic\SpreadsheetExport\ColumnTypes\NumericColumn;
use Fusonic\SpreadsheetExport\ColumnTypes\TextColumn;
use Fusonic\SpreadsheetExport\Writers\CsvWriter;
use Fusonic\SpreadsheetExport\Writers\TsvWriter;
use Fusonic\SpreadsheetExport\Writers\OdsWriter;

function bolsas_relatorios_bolsistas() {
	$export = new Spreadsheet();
	
	// colunas
	$export->AddColumn(new TextColumn("Nome", 5));
	$export->AddColumn(new TextColumn("CPF", 3));
	$export->AddColumn(new TextColumn("Modalidade", 3));
	$export->AddColumn(new TextColumn("Banco", 3));
	$export->AddColumn(new TextColumn("Agência", 3));
	$export->AddColumn(new TextColumn("Conta", 3));
	$export->AddColumn(new TextColumn("Início", 3));
	$export->AddColumn(new TextColumn("Fim", 3));
	$export->AddColumn(new TextColumn("Valor", 3));
	$export->AddColumn(new TextColumn("Status", 3));
	
	$query = db_query("SELECT a.nome, a.cpf, b.nome AS banco, a.agencia, a.conta, 
			m.nome AS modalidade, o.inicio, o.fim, m.valor, o.ativo
			FROM aluno AS a
			INNER JOIN banco AS b
			INNER JOIN bolsa AS o
			INNER JOIN modalidade AS m
			WHERE a.id = o.aluno_id
			AND b.id = a.banco_id
			AND m.id = o.modalidade_id;");
	
	$status = array(0 => "Inativo", 1 => "Ativo");
	foreach ($query as $row) {
		$export->AddRow( array(	$row->nome, $row->cpf, $row->modalidade, 
				$row->banco, $row->agencia, $row->conta, $row->inicio, $row->fim,
				$row->valor, $status[$row->ativo]) );
	}
	
	$writer = new OdsWriter();
	$writer->includeColumnHeaders = true;
	
	$export->download($writer, "RelatorioBolsistas_" . date("d-m-Y"));
}